<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
use lib\UserCrypt;
class Index extends Base
{
    // 系统设置
    public function systemSet()
    {
        $res=Db::name('system_set')->where('id',1)->find();
        if($res){
            $res['score_rule'] = str_replace('<img src="','<img src="'.$this->request->domain(),$res['score_rule']);
            $res['introduce'] = str_replace('<img src="','<img src="'.$this->request->domain(),$res['introduce']);
            $res['tips'] = str_replace('<img src="','<img src="'.$this->request->domain(),$res['tips']);
            $res['protocol'] = str_replace('<img src="','<img src="'.$this->request->domain(),$res['protocol']);
            return json(['code' => 200, 'msg' => '查询成功' ,'app_version'=>config('app_version'),'data' =>$res]);
        }else{
            return json(['code' => 500, 'msg' => '查无结果']);
        }
    }
  	//轮播图
	public function banner(){
        $list=Db::name('banner')->where('state','<',3)->order('id asc')->select();
        if($list){
            foreach ($list as $key => $value) {
                if($list[$key]['type']==1){
                    $list[$key]['img']=Db::name('product')->where('id',$list[$key]['url'])->value('img');
                    $list[$key]['title']=Db::name('product')->where('id',$list[$key]['url'])->value('title');
                }
            }
            return json(['code' => 200, 'msg' => '查询成功' ,'data' =>$list]);
        }else{
            return json(['code' => 500, 'msg' => '查无结果']);
        }
	}
    //产品详情
    public function productDetail(){
        if(!empty(input("param.token"))){
            $token=input("param.token");
        }else{
            return json(['code' => 501, 'msg' => '令牌呢？']);
        }
        if(!empty(input("param.id"))){
            $id=input("param.id");
        }else{
            return json(['code' => 502, 'msg' => '详情id呢？']);
        }
        $uid=UserCrypt::do_mdecrypt($token);
        $res=Db::name('product')->where('id',$id)->find();
        if($res){
            $isIn=Db::name('collects')->where('user_id',$uid)->where('p_id',$id)->where('state',1)->find();
            if($isIn){
                $res['collect_state']=1;
            }else{
                $res['collect_state']=0;
            }
            //config('http_https').$_SERVER['HTTP_HOST']
            $res['detail'] = str_replace('<img src="','<img src="'.$this->request->domain(),$res['detail']);
            $res['imgs']=explode(',',$res['imgs']);
            $res['price']=Db::name('spec_combination')->where('p_id',$id)->where('state',1)->order('id asc')->value('money');
            $res['spec']=Db::name('spec')->where('p_id',$id)->where('state',1)->order('id asc')->select();
            if ($res['spec']) {
                foreach ($res['spec'] as $key => $value) {
                    $res['spec'][$key]['spec_vals']=Db::name('spec_val')->where('p_id',$id)->where('spec_id',$res['spec'][$key]['id'])->where('state',1)->order('id asc')->select();
                }
            }
            $res['spec_combination']=Db::name('spec_combination')->where('p_id',$id)->where('state',1)->order('id asc')->select();
            $res['collects']=Db::name('collects')->where('p_id',$id)->where('state',1)->count();
            return json(['code' => 200, 'msg' => '查询成功' ,'data' =>$res]);
        }else{
            return json(['code' => 500, 'msg' => '查无结果']);
        }
    }
    // 最新与热销
    public function productHot(){
        if(input("param.hot_new")){
            $hot_new=input("param.hot_new");
        }else{
            return json(['code' => 501, 'msg' => '最新还是热销']);
        }
        $map = [
            ['state', '=', 1]
        ];
        if($hot_new==1){
            array_push($map,['new','=',1]);
        }elseif ($hot_new==2) {
            array_push($map,['hot','=',1]);
        }
        $orderby = 'sort desc';
        $products=Db::name('product')->where($map)->field('id,title,img')->order($orderby)->select();
        if($products){
            foreach ($products as $key => $value) {
                $products[$key]['price']=Db::name('spec_combination')->where('p_id',$products[$key]['id'])->where('state',1)->order('id asc')->value('money');
            }
            return json(['code' => 200, 'msg' => '成功！','data'=>$products]);
        }else{
            return json(['code' => 500, 'msg' => '查无数据！']);
        }
    }
    // 一级分类
    public function cates()
    {
        $cates=Db::name('cates')->where('f_id',0)->where('state',1)->select();
        if($cates){
            return json(['code' => 200, 'msg' => '查询成功' ,'data' =>$cates]);
        }else{
            return json(['code' => 500, 'msg' => '查无结果']);
        }
    }
    //子分类
    public function subCates()
    {
        if(input("param.f_id")){
            $f_id=input("param.f_id");
        }else{
            return json(['code' => 501, 'msg' => '一级分类的id呢']);
        }
        $res=Db::name('cates')->where('f_id',$f_id)->where('state',1)->select();
        if($res){
            return json(['code' => 200, 'msg' => '成功！','data'=>$res]);
        }else{
            return json(['code' => 500, 'msg' => '查无结果']);
        }
    }
}